var form = layui.form;
layer = parent.layer === undefined ? layui.layer : top.layer,
    $ = layui.jquery;
function sub(data){
    if(nameCheck()&&ageCheck()&&workYearCheck()&&abilityCheck()&&gradeCheck()){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.5});
        $.post("/staff/add",{
            user : $(".user").val(),
            name : $(".name").val(),
            age: $(".age").val(),
            workYear: $(".workYear").val(),
            ability : $(".ability").val(),
            post: $(".post").val(),
            grade : $(".grade").val()
        },function(res){
            if(res.code===0){
                layer.msg(res.msg,{
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                });
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            }else {
                layer.msg(res.msg);
            }
        })
    }else{
        event.preventDefault();
        return false;
    }
}
function nameCheck() {
    var reg = /[\u4e00-\u9fa5]/;
    var name = $(".name").val();
    if (!reg.test(name) || name==='') {
        layer.msg("请输入中文的员工姓名",{icon:5});
        return false;
    }else{
        return true;
    }
}
function ageCheck() {
    var reg = /^([1-9]\d?|1[01]\d|120)$/;
    var age = $(".age").val();
    if (!reg.test(age) || age==='') {
        layer.msg("请输入真实年龄",{icon:5});
        return false;
    }else{
        return true;
    }
}
function workYearCheck() {
    var reg = /^([1-9]\d?|1[01]\d|120)$/;
    var workYear = $(".workYear").val();
    var age = $(".age").val()-18;
    if (!reg.test(workYear) || workYear===''||workYear>age) {
        layer.msg("请输入正确的工作时间（正整数）",{icon:5});
        return false;
    }else{
        return true;
    }
}
function abilityCheck() {
    var ability = $(".ability").val();
    if (ability==='') {
        layer.msg("能力描述不能为空",{icon:5});
        return false;
    }else{
        return true;
    }
}
function gradeCheck() {
    var reg = /^(0|[1-9]+[0-9]*)(.[0-9]{1,4})?$/;
    var grade = $(".grade").val();
    if (!reg.test(grade) || grade===''||grade>10) {
        layer.msg("请输入不大于10的正数值评分",{icon:5});
        return false;
    }else{
        return true;
    }
}
